/**
 * Title:        Utility Class Library<p>
 * Description:  A text field class that forces numeric only input.<p>
 * @version 1.0
 */
package com.timber.util;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;


public class DoubleField extends JTextField
{
  private Object _source = null;

  public DoubleField()
  {
    super();
  }

  public DoubleField( String text )
  {
    super( text );
  }

  public DoubleField( int columns )
  {
    super( columns );
  }

  public DoubleField( String text, int columns )
  {
    super( text, columns );
  }

  public double getDouble() {
        String s = getText();

        if ( s == null || (s!=null && s.length() < 1)) return 0;

	return Double.parseDouble( getText() );
  }

  public void setSource(Object obj) {
	_source = obj;
  }
  public Object getSource() {
	return _source;
  }

  protected Document createDefaultModel()
  {
    return new DoubleDocument();
  }

  static class DoubleDocument extends PlainDocument
  {
    public void insertString( int offs, String str, AttributeSet a ) throws BadLocationException
    {
      if( str == null ) return;

      if (! ".".equals(str) ) {
          try {
            Double.parseDouble( str );
          }
          catch( Exception ex ) {
            return;
          }
      }

      super.insertString( offs, str, a );
    }
  }
}
